package com.codeZ.ObserverPattern.instance;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 事件处理类
 *
 * @author codeZhang
 * @Date 2024/8/26 11:05
 */
public class EventManager {

    public enum EventType{
        MQ,Message
    }
    //监听器集合
    Map<Enum<EventType>, List<EventListenerObserver>> listeners = new HashMap<>();

    public EventManager(List<Enum<EventType>> operations) {
        for (Enum<EventType> operation : operations) {
            this.listeners.put(operation,new ArrayList<>());
        }
    }

    /**
     * 订阅
     * @param eventType 事件类型
     * @param listener  监听
     */
    public void subscribe(Enum<EventType> eventType, EventListenerObserver listener){
        List<EventListenerObserver> users = listeners.get(eventType);
        users.add(listener);
    }

    /**
     * 取消订阅
     * @param eventType 事件类型
     * @param listener  监听
     */
    public void unsubscribe(Enum<EventType> eventType,EventListenerObserver listener){
        List<EventListenerObserver> users = listeners.get(eventType);
        users.remove(listener);
    }

    /**
     * 通知
     * @param eventType 事件类型
     * @param result    结果
     */
    public void notify(Enum<EventType> eventType, LotteryResult result){
        List<EventListenerObserver> users = listeners.get(eventType);
        for (EventListenerObserver listener : users) {
            listener.doEvent(result);
        }
    }

}
